package com.aoxin.controller.course.webCourse;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aoxin.model.course.SlfMyCourse;
import com.aoxin.service.inter.course.webCourse.MyCourseService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.EmptyUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.enumerate.UserType;
import com.aoxin.util.log.LogInfo;

@RestController
@RequestMapping("/web/myCourse/")
public class MyCourseController {

	@Autowired
	private MyCourseService myCourseService;

	/**
	 * 新增我的课程
	 * 
	 * @param courseId
	 * @param res
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.INSERT, userType = UserType.USER)
	@RequestMapping("insertMyCourse")
	public AjaxRes insertMyCourse(Integer courseId, Integer syllabusId, Integer childrenId, AjaxRes res) {
		try {
			this.myCourseService.insertMyCourse(courseId, syllabusId, childrenId);
			res.setSucceedMsg("insertMyCourse success!");
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 查询我的全部课程|查询某个孩子课程
	 * 
	 * @param slfMyCourse
	 *            ,res,pageInfo
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.QUERY, userType = UserType.USER)
	@RequestMapping("getMyCourse")
	public AjaxRes getMyCourse(SlfMyCourse slfMyCourse, AjaxRes res) {
		List<SlfMyCourse> MyCourses = myCourseService.getMyCourse(slfMyCourse);
		res.setSucceed(MyCourses, "getMyCourse success!");
		return res;
	}

	/**
	 * 删除我的课程
	 * 
	 * @param myCourseId
	 * @param res
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.DELETE)
	@RequestMapping("deleteMyCourseByMyCourseId")
	public AjaxRes deleteMyCourseByMyCourseId(Integer myCourseId, AjaxRes res) {
		if (myCourseId != null) {
			Integer result = myCourseService.deleteMyCourse(myCourseId);
			if (result != 0) {
				res.setSucceed("deleteMyCourseByMyCourseId success!");
			} else {
				res.setFailMsg("deleteMyCourseByMyCourseId Fail!");
			}
		} else {
			res.setFailMsg("myCourseId is null");
		}
		return res;
	}

	/**
	 * 批量删除我的课程
	 * 
	 * @param myCourseIds
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.DELETE)
	@RequestMapping("deleteMyCourseByMyCourseIds")
	public AjaxRes deleteMyCourseByMyCourseIds(@RequestParam("myCourseIds") List<Integer> myCourseIds, AjaxRes res) {
		if (myCourseIds != null && myCourseIds.size() > 0) {
			Integer result = myCourseService.deleteMyCourses(myCourseIds);
			if (result != 0) {
				res.setSucceed("deleteMyCourseByMyCourseIds success!");
			} else {
				res.setFailMsg("deleteMyCourseByMyCourseIds Fail!");
			}
		} else {
			res.setFailMsg("myCourseIds is null");
		}
		return res;
	}

	/**
	 * 修改我的课程
	 * 
	 * @param slfMyCourse
	 * @param res
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.UPDATE)
	@RequestMapping("updateMyCourse")
	public AjaxRes updateMyCourse(SlfMyCourse slfMyCourse, AjaxRes res) {
		try {
			if (!EmptyUtil.objFieldsIsEmpty(slfMyCourse)) {
				Integer result = myCourseService.updateMyCourse(slfMyCourse);
				if (result != 0) {
					res.setSucceed("updateMyCourse success!");
				} else {
					res.setFailMsg("updateMyCourse Fail!");
				}
			} else {
				res.setFailMsg("slfMyCourse is null");
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return res;
	}

	// TODO:新增消费记录

}
